/* Utilities.cs

	Infinite Rogue
	By Dan Alexander, Andy Law
	July, 2012

	Cross platform roguelike using C#, Mono, WinForms and GDI.
	
	LICENSE: New BSD License
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public static class Utilities{

	//unique tag for each instance of the game. Should be accessed by calling NextTag().
	private static ulong _lastTag = 0;
	
	//will increment _lastTag and return the new, unique tag
	public static ulong NextTag(){
		++_lastTag;
		return _lastTag;
	}
	
	//This is a global Random, which should be used throughout the engine. This way
	//you can seed (used for debug and multiplayer)
	public static Random GlobalRandom = new Random((int)DateTime.Now.Ticks);

    public static bool Between(this int x, int a, int b)
    {

        return x >= a && x <= b;

    }

    public static int DistanceTo(this MapCoord a, MapCoord b)
    {
        return Map.Distance(a.x, a.y, b.x, b.y);
    }

    public static bool TakeChance(float probabilityRatio)
    {
        int precisionMultiplier = 100;
        int p = (int)(probabilityRatio * 100f * (float)precisionMultiplier);
        int selection = GlobalRandom.Next(100 * precisionMultiplier);
        // Less than and not equal to. If probabilityRatio is 0, no selection is less than 0. 
        // If probabilityRatio is 1, the call Random.Next always returns a number less than the parameter
        return selection < p;
    }

    public static int RollDice(int numDice, int sides)
    {
        int sum = 0;
        for (int i = 0; i < numDice; ++i)
        {
            sum += GlobalRandom.Next(sides) + 1;
        }
        return sum;
    }

    public static Tuple<int, int>[] Moves = new Tuple<int, int>[]
    {
        Tuple.Create<int, int>(-1, -1),
        Tuple.Create<int, int>(-1, 0),
        Tuple.Create<int, int>(-1, 1),
        Tuple.Create<int, int>(0, -1),
        Tuple.Create<int, int>(0, 1),
        Tuple.Create<int, int>(1, -1),
        Tuple.Create<int, int>(1, 0),
        Tuple.Create<int, int>(1, 1),
    };

    public static Tuple<int, int> RandomMove()
    {
        return Moves[GlobalRandom.Next(8)];
    }

    public static TSource Random<TSource>(this IEnumerable<TSource> list)
    {
        return list.ElementAt(GlobalRandom.Next(list.Count()));
    }

}